<!-- 新增/编辑指标 -->
<template>
  <a-drawer
    width="45%"
    :visible="props.visible"
    @ok="handleOk"
    @cancel="handleCancel"
    draggable
    title-align="center"
    unmountOnClose
  >
    <template #title> {{ props.data?.id ? '编辑指标' : '新增指标' }} </template>
    <div>
      <a-form :model="formData" ref="refForm" :rules="rules">
        <a-form-item label="指标名称" field="name" show-colon>
          <a-input v-model="formData.name"></a-input>
        </a-form-item>
        <a-form-item label="指标定义" field="definition" show-colon>
          <a-textarea
            v-model="formData.definition"
            show-word-limit
            :max-length="50"
            :auto-size="{
              minRows: 4
            }"
          ></a-textarea>
        </a-form-item>
        <a-form-item label="项目界定" field="define" show-colon>
          <a-textarea
            v-model="formData.define"
            show-word-limit
            :max-length="50"
            :auto-size="{
              minRows: 4
            }"
          ></a-textarea>
        </a-form-item>
        <a-form-item label="评分标准/规则" field="rule" show-colon>
          <a-textarea
            v-model="formData.rule"
            show-word-limit
            :max-length="50"
            :auto-size="{
              minRows: 4
            }"
          ></a-textarea>
        </a-form-item>
        <!-- <a-form-item label="指标权重" field="indexWeight" show-colon>
          <a-input-number v-model="formData.indexWeight" hide-button>
            <template #suffix>%</template></a-input-number
          >
        </a-form-item> -->
        <a-form-item
          label="考核目标值"
          field="assessmentTargetValue"
          show-colon
        >
          <a-input-number v-model="formData.assessmentTargetValue" hide-button>
          </a-input-number>
        </a-form-item>
        <a-form-item
          label="考核目标单位"
          field="assessmentTargetUnit"
          show-colon
        >
          <a-input v-model="formData.assessmentTargetUnit"></a-input>
        </a-form-item>
        <!-- <a-form-item label="状态" field="" show-colon>
          <a-space>
            <a-switch
              v-model="formData.status"
              :checked-value="1"
              :unchecked-value="2"
            ></a-switch>
          </a-space>
        </a-form-item> -->
      </a-form>
    </div>
    <template #footer>
      <a-space>
        <a-button @click="handleCancel">取消</a-button>
        <a-button type="primary" @click="handleOk">确认</a-button>
        <a-button v-if="isShowContinueBtn" type="primary" @click="continueAdd"
          >确认并继续新增</a-button
        >
      </a-space>
    </template>
  </a-drawer>
</template>

<script lang="ts" setup>
import { cloneDeep } from 'lodash';
import { PerformanceKeyPerformanceIndexLibraryUse } from '@/apis/management/performance/baseSetting/template/type';

const props = withDefaults(
  defineProps<{
    visible: boolean; // 是否显示对话框
    data?: PerformanceKeyPerformanceIndexLibraryUse; // 传进来的数据,
  }>(),
  {}
);
const tempData = [{}];
const arr = [
  {
    value: 1,
    label: '财务类'
  },
  {
    value: 2,
    label: '客户类'
  },
  {
    value: 3,
    label: '内部运营类'
  },
  {
    value: 4,
    label: '学习与成长类'
  }
];
const polarityList = [
  { value: 1, lable: '越大越好' },
  { value: 2, lable: '越小越好' }
];
const emits = defineEmits(['update:visible', 'submit']);
let refForm = ref();

let isShowContinueBtn = ref(true); // 是否显示继续新增按钮
const formData = ref<PerformanceKeyPerformanceIndexLibraryUse>({});
/**
 * 查重
 */
const validateName = async (value: any, cb: any) => {
  //   if (value === props.data?.name) return;
  //   const res = await GetPpiIndexRepeat({
  //     name: value
  //   });
  //   if (res?.code === 400) {
  //     cb('名称已存在!');
  //   }
};
const rules = reactive({
  name: [
    {
      required: true,
      message: '请输入'
    },
    {
      //   validator: validateName
    }
  ],
  indexWeight: [
    {
      required: true,
      message: '请输入'
    }
  ]
});

watch(
  () => props.visible,
  (newVal: boolean) => {
    // 如果是编辑，则赋值给formData
    formData.value = cloneDeep(props.data);
  }
);

const handleOk = async () => {
  if (await refForm.value.validate()) {
    return;
  }
  emits('submit', formData.value);
  emits('update:visible', false);
};

const handleCancel = () => {
  emits('update:visible', false);
};
</script>

<style scoped lang="less">
.icon {
  cursor: pointer;
  font-size: 20px;
}
</style>
